package com.SinaMicroblogging.TabFocused;

import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import com.SinaMicroblogging.PageBase;
import com.SinaMicroblogging.R;
import com.SinaMicroblogging.Extension.UserInfoItemAdapter;
import com.SinaMicroblogging.UI.BlockView;
import com.SinaMicrobloggingApi.Core.Process.SinaMicrobloggingAPIProcess;
import com.SinaMicrobloggingApi.Model.UserInfoModel;
import com.Util.HttpUtility;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PersonalTabFocused extends TabFocusedBase {
	BlockView header;
	boolean firstEnter = true;
	private ListView listView;
	UserInfoItemAdapter userInfoItemAdapter;
	private Handler handler;
	ImageView avatar;

	public void OnTabFocused(PageBase page) {
		// TODO Auto-generated method stub
		super.OnTabFocused(page);
		if (firstEnter) {
			handler = createHandler();
			this.page = page;
			this.context = page.getApplicationContext();
			UserInfoModel user = page.getCurrentUser();
			avatar = (ImageView) page.findViewById(R.id.useravatar);
			avatar.setImageResource(R.drawable.defaultavatar);
			TextView userName = (TextView) page.findViewById(R.id.username);
			userName.setText(user.getScreen_name());
			TextView tagline = (TextView) page.findViewById(R.id.tagline);
			tagline.setText(user.getDescription());
			initUserInfoList(user);
			getUserAvatar();
			firstEnter = false;
		}
	}

	private Handler createHandler() {
		Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					if (page.getCurrentUser().getAvatarImg() != null) {
						avatar.setImageBitmap(page.getCurrentUser()
								.getAvatarImg());
					}
					break;
				default:
					break;
				}
			}
		};
		return handler;
	}

	void getUserAvatar() {
		new Thread(new Runnable() {

			@SuppressWarnings("unchecked")
			@Override
			public void run() {
				// TODO Auto-generated method stub

				UserInfoModel user = PersonalTabFocused.this.page
						.getCurrentUser();
				String urlKey = user.getProfile_image_url();
				Bitmap userAvatar = null;
				SoftReference<Bitmap> bitmapRef;
				bitmapRef = (SoftReference<Bitmap>) cache.get(urlKey);
				if (bitmapRef == null) {
					try {
						userAvatar = HttpUtility.getBitmap(urlKey);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.i("sinaweibo", "getlogginguseravatar");
					cache.put(urlKey, userAvatar);
				} else {
					userAvatar = bitmapRef.get();
				}
				user.setAvatarImg(userAvatar);
				Message message = new Message();
				message.what = 1;
				PersonalTabFocused.this.getHandler().sendMessage(message);
			}

		}).start();
	}

	public Handler getHandler() {
		return handler;
	}

	private void initUserInfoList(UserInfoModel user) {
		listView = (ListView) page.findViewById(R.id.userinfo);
		List<String> userInfos = new ArrayList<String>();
		userInfos.add(String.format("%s_%s", "My Friends", user
				.getFriends_count()));
		userInfos.add(String.format("%s_%s", "My Followers", user
				.getFollowers_count()));
		userInfos.add(String.format("%s_%s", "My Favourites", user
				.getFavourites_count()));
		userInfos.add(String.format("%s_%s", "My Statues", user
				.getStatuses_count()));
		userInfoItemAdapter = new UserInfoItemAdapter(userInfos, super.context);
		listView.setAdapter(userInfoItemAdapter);
	}
}
